package services

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"math/rand"
	"student/user-srv/basic/config"
	"student/user-srv/basic/inits"
	__ "student/user-srv/basic/proto"
	"student/user-srv/modle"
)

type Server struct {
	__.UnimplementedUserServer
}

// 超级管理员登录
// SayHello implements helloworld.GreeterServer
func (s *Server) SuperLogin(_ context.Context, in *__.SuperUserLoginReq) (*__.SuperUserLoginResp, error) {
	var superUser modle.SuperUser
	config.DB.Where("mobile = ?", in.Mobile).Find(&superUser)
	if superUser.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}
	if in.Password != superUser.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &__.SuperUserLoginResp{
		UserId: int64(superUser.ID),
	}, nil
}

func (s *Server) SchoolAdd(_ context.Context, in *__.SchoolAddReq) (*__.SchoolAddResp, error) {
	var school = modle.School{
		Name:    in.Name,
		Phone:   in.Phone,
		Address: in.Address,
		Img:     in.Img,
		Clams:   in.Clams,
		Status:  "正常",
	}

	config.DB.Create(&school)
	return &__.SchoolAddResp{
		SchoolId: int64(school.ID),
	}, nil
}
func (s *Server) SchoolList(_ context.Context, in *__.SchoolListReq) (*__.SchoolListResp, error) {
	var school []modle.School
	db := config.DB.Scopes(Paginate(int(in.Page), int(in.Size))).Order("created_at desc")
	if in.Name != "" {
		db = db.Where("name like ?", "%"+in.Name+"%")
	} else {
		if in.Status != "" {
			db = db.Where("name like ?", "%"+in.Name+"%")
		}
	}
	db.Find(&school)
	var b []*__.SchoolItem
	for _, c := range school {
		list := __.SchoolItem{
			Name:    c.Name,
			Phone:   c.Phone,
			Address: c.Address,
			Img:     c.Img,
			Clams:   c.Clams,
			Status:  c.Status,
		}
		b = append(b, &list)
	}
	return &__.SchoolListResp{
		List: b,
	}, nil
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
func (s *Server) UserAdd(_ context.Context, in *__.UserAddReq) (*__.UserAddResp, error) {
	var user = modle.User{
		Mobile:   in.Mobile,
		Clams:    in.Clams,
		SchoolID: int(in.SchoolId),
	}
	config.DB.Create(&user)
	return &__.UserAddResp{
		UserId: int64(user.ID),
	}, nil
}
func (s *Server) SendSms(_ context.Context, in *__.SendSmsReq) (*__.SendSmsResp, error) {
	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	inits.Rdb.Set(inits.Ctx, "sendSms"+in.Mobile, code, 0)
	return &__.SendSmsResp{}, nil
}
func (s *Server) UserList(_ context.Context, in *__.UserListReq) (*__.UserListResp, error) {
	var user []modle.User
	db := config.DB
	if in.Mobile != "" {
		db = db.Where("mobile like ?", "%"+in.Mobile+"%")
	}
	db.Find(&user)
	var b []*__.UserItem
	for _, c := range user {
		list := __.UserItem{
			Mobile:   c.Mobile,
			SchoolId: int64(c.SchoolID),
			Clams:    c.Clams,
		}
		b = append(b, &list)
	}
	return &__.UserListResp{
		List: b,
	}, nil
}
